package com.fudan.framework.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;

import com.fudan.framework.base.BaseController.PageBean;
import com.fudan.framework.base.i.IDaoService;

/**
 * Dao的基类，封装dao的常用操作
 * @author Xuxinxing
 *
 * @param <T> 数据库实体
 */
public abstract class DaoService<T> extends BaseService implements IDaoService<T>{

	abstract protected BaseDao<T> injectDao();
	@Transactional
	public Boolean add(T t) {
		BaseDao<T> dao = injectDao();
		fill(t);
		dao.save(t);
		return true;
	}
	@Transactional
	public Boolean addAll(Iterable<T> entities) {
		BaseDao<T> dao = injectDao();
		dao.saveAll(entities);
		return true;
	}
	@Transactional
	public Boolean update(T t) {
		BaseDao<T> dao = injectDao();
		fill(t);
		dao.save(t);
		return true;
	}
	@Transactional
	public Boolean updateBatch(List<T> entities) {
		BaseDao<T> dao = injectDao();
		dao.saveAll(entities);
		return true;
	}

	protected void fill(T t) {
	}

	public T get(Object id) {
		BaseDao<T> dao = injectDao();
		return dao.getById(Integer.parseInt(String.valueOf(id)));
	}
	@Transactional
	public Boolean delete(String id) {
		BaseDao<T> dao = injectDao();
		dao.deleteById(Integer.parseInt(id));
		return true;
	}
	@Transactional
	public Boolean deleteBatch(List<T> entities) {
		BaseDao<T> dao = injectDao();
		dao.deleteAll(entities);
		return true;
	}

	public long countAll() {
		BaseDao<T> dao = injectDao();
		return dao.count();
	}

	public List<T> pages(HttpServletRequest request) {
		PageBean pageBean = PageBean.of(request);
		return pages(pageBean.getPageIndex(),pageBean.getSize());
	}

	public List<T> findAllByFuzzyQuery(HttpServletRequest request) {
		String pageStr = request.getParameter("page");
		String sizeStr = request.getParameter("limit");
		int page = 1;
		int size = 10;
		if(pageStr!=null && pageStr.matches("[0-9]+")) {
			page = Integer.parseInt(pageStr);
		}
		if(sizeStr!=null && sizeStr.matches("[0-9]+")) {
			size = Integer.parseInt(sizeStr);
		}
		String field = request.getParameter("search_field");
		String value = request.getParameter("search_value");
		if(field!=null&&!"".equals(field)&&value!=null&&!"".equals(value)) {
			return findAllByFuzzyQuery(field, value, page-1, size);
		}else {
			return pages(page-1, size);
		}
	}
	
	public List<T> findAll() {
		BaseDao<T> dao = injectDao();
		return dao.findAll();
	}

	public List<T> findAllByFuzzyQuery(String fieldName,String value,int page,int size) {
		BaseDao<T> dao = injectDao();
		if(fieldName==null||value==null||"".equals(fieldName)||"".equals(value)) {
			return dao.findAllWithPage(page, size);
		}else {
			return dao.findAllWithTermAndAbbr(fieldName, value, page, size);
		}
	}

	public List<T> findAllByFuzzyQuery(String fieldName,String value,PageBean pageBean) {
		return findAllByFuzzyQuery(fieldName, value, pageBean.getPage()-1, pageBean.getSize());
	}

	private List<T> pages(int page,int size) {
		BaseDao<T> dao = injectDao();
		return dao.findAllWithPage(page, size);
	}
	
	public List<T> pageWithExample(HttpServletRequest request,T example) {
		String pageStr = request.getParameter("page");
		String sizeStr = request.getParameter("limit");
		int page = 1;
		int size = 10;
		if(pageStr!=null && pageStr.matches("[0-9]+")) {
			page = Integer.parseInt(pageStr);
		}
		if(sizeStr!=null && sizeStr.matches("[0-9]+")) {
			size = Integer.parseInt(sizeStr);
		}
		BaseDao<T> dao = injectDao();
		return dao.findAllWithPageAndCondition(page, size, example);
	}
	
	public List<T> getAllByExample(T condition) {
		BaseDao<T> dao = injectDao();
		return dao.findAllWithCondition(condition);
	}
	
	public long countWithExample(T example) {
		BaseDao<T> dao = injectDao();
		return dao.countWithCondition(example);
	}

}
